<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/3/8
 * Time: 13:46
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Xun extends Base
{
    /**
     * 询价单首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $xun_username = input('xun_username');
            $xun_phone = input('xun_phone');

            $where = [];
            if($xun_username) $where['xun_username'] = ['LIKE', '%' . $xun_username . '%'];
            if($xun_phone) $where['xun_phone'] = ['LIKE', '%' . $xun_phone . '%'];

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_xun')
                ->join('up_u_user', 'xun_user_id = user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_xun')
                ->join('up_u_user', 'xun_user_id = user_id', 'LEFT')
                ->where($where)
                ->field([
                    'xun_id',
                    'xun_username',
                    'xun_phone',
                    'xun_isRead',
                    'xun_time',
                    'user_xing',
                    'user_name',
                    'xun_reply_time'
                ])
                ->page($Nowpage, $limits)
                ->order('xun_isRead ASC, xun_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 询价单详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function xunDetail(){
        $xun_id = input('xun_id');
        $data = Db::table('up_xun')
            ->join('up_u_user', 'user_id = xun_user_id', 'LEFT')
            ->where('xun_id', $xun_id)
            ->field('user_xing, user_name, xun_username, xun_phone, xun_desc, xun_reply, xun_isRead, xun_time, xun_reply_time')
            ->find();
        return $this->fetch('', ['data' => $data]);
    }

    /**
     * 回复
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function xunReply(){
        $xun_id = input('xun_id');
        if(Request::instance()->isPost()){
            $reply = input('reply');

            $update = [
                'xun_reply' => $reply,
                'xun_isRead' => 1,
                'xun_reply_time' => initTime()
            ];
            $res = Db::table('up_xun')->where('xun_id', $xun_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch('', ['xun_id' => $xun_id]);
    }
}